import ResUtil from "@/utils/resUtil";
import MediatorsOrganization from "@/types/response/mediatorsOrganization";
import axiosManager from "@/axios/axiosManager";
import Mediator from "@/types/response/mediator";
import MediatorComment from "@/types/response/mediatorComment";
import MediatorSchedulingTime from "@/types/response/mediatorSchedulingTime";
import mediator from "@/types/response/mediator";
import mediatorsOrganization from "@/types/response/mediatorsOrganization";
import type MediatorToOrganization from "@/types/response/mediatorToOrg";

let request = axiosManager.mediatorAxios;

// 响应拦截器
// request.interceptors.response.use(response => {
//     if (response.data.code === 200) {
//         return response.data.data;
//     } else {
//          console.log('error: '+ response.data.message)
//     }
//
// }, error => {
//     console.log(error);
// })

// 增加调解员
async function addMediator(mediator: Mediator) {
    console.log(mediator)
    let result = await request({
        method: 'post',
        url: "/mediator",
        data: mediator,
    })
    return ResUtil.getData<Mediator>(result)
}

// 获取所有调解员
async function getAllMediators() {
    
    return ResUtil.getData<Mediator[]>(await request.get('/mediators'))
}


//根据手机号获取调解员信息
async function getMediatorByPhone(phone: string) {
    return ResUtil.getData<Mediator>(await request({
            method: 'get',
            url: '/mediator/phone',
            params: {
                phone: phone
            }
        })
    )
}

//新增调解员组织
async function addMediatorOrganization(mediatorsOrganization: MediatorsOrganization) {
    return ResUtil.getData<mediatorsOrganization>(await request({
            method: 'post',
            url: '/mediatorOrganization',
            data: mediatorsOrganization
        })
    )
}

//根据组织id删除调解员组织,组织下有调解员不能删除
async function deleteMediatorOrganization(id: number) {
    return ResUtil.getData(await request.delete('/mediatorOrg?orgId=' + id))
}


//修改调解员信息
async function updateMediator(mediator: Mediator) {
    let rres=await request({
        method: 'put',
        url: '/mediator',
        data: mediator
    });
    return ResUtil.isSuccess(rres);
}

//根据id获取调解员信息
async function getMediatorById(id: number) {
    return ResUtil.getData<Mediator>(await request({
            method: 'get',
            url: '/mediator/get',
            params: {
                mId: id
            }
        })
    )
}

//对调解员进行评论
async function addMediatorComment(mediatorComment: MediatorComment) {
    return ResUtil.getData(await request({
            method: 'post',
            url: '/mediator/comment',
            data: mediatorComment
        })
    )
}

//根据调解员id获取对调解员的评论
async function getCommentByMediatorId(id: number) {
    return ResUtil.getData<MediatorComment>(await request({
            method: 'get',
            url: '/mediator/comment',
            params: {
                mId: id
            }
        })
    )
}

//根据调解员id获取其组织
async function getOrganizationByMediatorId(id: number) {
    return ResUtil.getData<mediatorsOrganization>(await request({
            method: 'get',
            url: '/mediatorInOrg',
            params: {
                mId: id
            }
        })
    )
}

//增加调解员和组织的关联关系
async function addMediatorToOrganization(mediatorId: number, organizationId: number) {
    return ResUtil.getData<MediatorToOrganization>(await request.post('/mediatorOrg?mId=' + mediatorId + '&orgId=' + organizationId))
}

//删除调解员和组织的关联关系
async function deleteMediatorToOrganization(mediatorId: number, organizationId: number) {
    return ResUtil.getData(await request.delete('/mediatorOrg?mId=' + mediatorId + '&orgId=' + organizationId))
}

//根据组织id获取其下所有调解员
async function getMediatorsByOrganizationId(id: number) {
    return ResUtil.getData<Mediator[]>(await request({
            method: 'get',
            url: '/mediatorsInOrg',
            params: {
                orgId: id
            }
        })
    )
}

//新增调解员调期
async function addMediatorSchedule(mediatorSchedule: MediatorSchedulingTime) {
    return ResUtil.getData<null>(await request({
            method: 'post',
            url: '/mediator/schedule',
            data: mediatorSchedule
        })
    )
}

//修改调解员调期
async function updateMediatorSchedule(mediatorSchedule: MediatorSchedulingTime) {
    return ResUtil.getData(await request({
            method: 'put',
            url: '/mediator/schedule',
            data: mediatorSchedule
        })
    )
}

//根据调解员id获取其调期
async function getScheduleByMediatorId(id: number) {
    return ResUtil.getData(await request({
            method: 'get',
            url: '/mediator/schedule',
            params: {
                mId: id
            }
        })
    )
}

//根据调解员id删除其调期
async function deleteScheduleByMediatorId(id: number) {
    return ResUtil.getData(await request.delete('/mediator/schedule?mId=' + id))
}
//获得所有的调解组织
async function getAllOrganizations() {
    let res=await request.get('/admin/getAllOrganization')
    return ResUtil.getData<mediatorsOrganization[]>(res)
}
//更改调解组织的信息
async function updateOrganization(organization: mediatorsOrganization) {
    let res=await request({
        method: 'put',
        url: '/admin/updateOrganization',
        data: organization
    })
    return ResUtil.isSuccess(res)
}
//筛选案件

export {
    getAllOrganizations,
    updateOrganization,
    addMediator,
    getAllMediators,
    getMediatorByPhone,
    addMediatorOrganization,
    deleteMediatorOrganization,
    updateMediator,
    getMediatorById,
    addMediatorComment,
    getCommentByMediatorId,
    getOrganizationByMediatorId,
    addMediatorToOrganization,
    deleteMediatorToOrganization,
    getMediatorsByOrganizationId,
    addMediatorSchedule,
    updateMediatorSchedule,
    getScheduleByMediatorId,
    deleteScheduleByMediatorId
}